/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.yourorghere.module5;

import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.io.Serializable;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JToggleButton;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;
//import org.openide.util.Utilities;

class PaintTopComponent extends TopComponent {
 private javax.swing.JButton Clear;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private static PaintTopComponent instance;
    PaintCanvas c=new PaintCanvas();
      private javax.swing.JComboBox Color;
      
    JButton clear = new JButton();
    /** path to the icon used by the component and its open action */
//    static final String ICON_PATH = "SET/PATH/TO/ICON/HERE";

    private static final String PREFERRED_ID = "PaintTopComponent";

    private JButton jToggleButton1;
    private JLabel jLabel2;
    private JButton Client;
    private JLabel jLabel3;

    private PaintTopComponent() {
        initComponents();
        setName(NbBundle.getMessage(PaintTopComponent.class, "CTL_PaintTopComponent"));
        setToolTipText(NbBundle.getMessage(PaintTopComponent.class, "HINT_PaintTopComponent"));
//        setIcon(Utilities.loadImage(ICON_PATH, true));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    /**
     * Gets default instance. Do not use directly: reserved for *.settings files only,
     * i.e. deserialization routines; otherwise you could get a non-deserialized instance.
     * To obtain the singleton instance, use {@link findInstance}.
     */
    public static synchronized PaintTopComponent getDefault() {
        if (instance == null) {
            instance = new PaintTopComponent();
        }
        return instance;
    }

    /**
     * Obtain the PaintTopComponent instance. Never call {@link #getDefault} directly!
     */
    public static synchronized PaintTopComponent findInstance() {
        TopComponent win = WindowManager.getDefault().findTopComponent(PREFERRED_ID);
        if (win == null) {
            Logger.getLogger(PaintTopComponent.class.getName()).warning(
                    "Cannot find " + PREFERRED_ID + " component. It will not be located properly in the window system.");
            return getDefault();
        }
        if (win instanceof PaintTopComponent) {
            return (PaintTopComponent) win;
        }
        Logger.getLogger(PaintTopComponent.class.getName()).warning(
                "There seem to be multiple components with the '" + PREFERRED_ID +
                "' ID. That is a potential source of errors and unexpected behavior.");
        return getDefault();
    }

    @Override
    public int getPersistenceType() {
        return TopComponent.PERSISTENCE_ALWAYS;
    }

    @Override
    public void componentOpened() {
        
        /////////////////////////////////////////////////

         jPanel1 = new javax.swing.JPanel();
        Clear = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jToggleButton1 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        Client = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
       
        org.openide.awt.Mnemonics.setLocalizedText(Clear, "Clear");
        Clear.addActionListener(new java.awt.event.ActionListener() {
        
            public void actionPerformed(java.awt.event.ActionEvent evt) {
               clearActionPerformed(evt);
            }
        });
       
        jLabel1.setFont(new java.awt.Font("Monospaced", 0, 12));
        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, "White Board ");

        org.openide.awt.Mnemonics.setLocalizedText(jToggleButton1, "Share");
        jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
            private int sharing;
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton1ActionPerformed(evt);
            }

            private void jToggleButton1ActionPerformed(ActionEvent evt) {
                 if(sharing==0)
          {
              sharing=c.shareServer();
              Client.setVisible(false);
              return;
          }
          else if(sharing==1)
          {
              c.stopshare();
              sharing=0;
              Client.setVisible(true);
          }
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jLabel2, "Server");

        org.openide.awt.Mnemonics.setLocalizedText(Client, "Share");
        Client.addActionListener(new java.awt.event.ActionListener() {
            private int sharing;
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ClientActionPerformed(evt);
            }

            private void ClientActionPerformed(ActionEvent evt) {
                if(sharing==0)
          {
              sharing=c.shareClient();
              return;
          }
          else if(sharing==1)
          {
              c.stopshare();
              sharing=0;
          }
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jLabel3, "Client");

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(Clear)
                .add(30, 30, 30)
                .add(jLabel2)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jToggleButton1)
                .add(18, 18, 18)
                .add(jLabel3)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(Client)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jLabel1)
                .addContainerGap(215, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                .add(Clear)
                .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 19, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(Client)
                .add(jLabel3)
                .add(jToggleButton1)
                .add(jLabel2))
        );

        org.jdesktop.layout.GroupLayout cLayout = new org.jdesktop.layout.GroupLayout(c);
        c.setLayout(cLayout);
        cLayout.setHorizontalGroup(
            cLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 619, Short.MAX_VALUE)
        );
        cLayout.setVerticalGroup(
            cLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 321, Short.MAX_VALUE)
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .add(c, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(c, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

            
        ///////////////////////////////////////////////
        
     
    }
    
    private void clearActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        c.clear();
        if(c.conn==1){
        Point cp=new Point(-1,-1);
        c.send(cp);
        }
    }
    @Override
    public void componentClosed() {
    // TODO add custom code on component closing
    }

    /** replaces this in object stream */
    @Override
    public Object writeReplace() {
        return new ResolvableHelper();
    }

    @Override
    protected String preferredID() {
        return PREFERRED_ID;
    }

    final static class ResolvableHelper implements Serializable {

        private static final long serialVersionUID = 1L;

        public Object readResolve() {
            return PaintTopComponent.getDefault();
        }
    }

}
